package com.imooc.o2o.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imooc.o2o.dao.AnalysisDao;
import com.imooc.o2o.dto.AnalysisExecution;
import com.imooc.o2o.dto.ReachDescExecution;
import com.imooc.o2o.entity.Analysis;
import com.imooc.o2o.entity.ReachDesc;
import com.imooc.o2o.entity.ReachPoint;
import com.imooc.o2o.enums.AnalysisEnum;
import com.imooc.o2o.enums.ReachDescEnum;
import com.imooc.o2o.excptions.AnalysisOperationException;
import com.imooc.o2o.excptions.ExcelOperationException;
import com.imooc.o2o.excptions.ReachDescOperationException;
import com.imooc.o2o.service.AnalysisService;

@Service
public class AnalysisServiceImpl implements AnalysisService{
	@Autowired
	private AnalysisDao analysisDao;
	@Override
	public List<Analysis> getAnalysisList(long userId) {
		return analysisDao.queryAnalysisByExcel(userId);
	}
	@Override
	public Analysis getAnalysisByExcelId(long excelId) {
		return analysisDao.queryAnalysisByExcelId(excelId);
	}
	@Override
	public AnalysisExecution deleteAnalysisById(Integer analysisId, long excelId) {
		try {
			int effectedNum=analysisDao.deleteAnalysis(analysisId,excelId);
			if(effectedNum<=0) {
				throw new AnalysisOperationException("课程成绩分析信息删除失败");
			}else {
				return new AnalysisExecution(AnalysisEnum.SUCCESS);
			}
		}catch (Exception e) {
			throw new AnalysisOperationException("deleteAnalysis error:"+e.getMessage());
		}
	}
	
	@Override
	public AnalysisExecution modifyAnalysis(Analysis analysis) {
		if(analysis==null||analysis.getAnalysisId()==null) {
			return new AnalysisExecution(AnalysisEnum.EMPTY_LIST);
		}else {
			try {
			int effectedNum=analysisDao.updateAnalysis(analysis);
			if(effectedNum<=0) {
				return new AnalysisExecution(AnalysisEnum.INNER_ERROR);
			}else {
				analysis=analysisDao.queryByAnalysisId(analysis.getAnalysisId());
				return new AnalysisExecution(AnalysisEnum.SUCCESS,analysis);
			}
			}catch (Exception e) {
				throw new AnalysisOperationException("modifyAnalysis error"+e.getMessage());
			}
		}
	}
	
	@Override
	public Analysis getByAnalysisId(int analysisId) {
		return analysisDao.queryByAnalysisId(analysisId);
	}
	
	@Override
	public AnalysisExecution InsertAnalysis(Analysis analysis) {
		if(analysis!=null) {
			try {
				int effectedNum=analysisDao.batchInsertAnalysis(analysis);
				if(effectedNum<=0) {
					throw new AnalysisOperationException("成绩分析信息创建失败");
				}else {
					return new AnalysisExecution(AnalysisEnum.SUCCESS);
				}
			}catch (Exception e) {
				throw new AnalysisOperationException("bathAddReachDesc error:"+e.getMessage());
			}
		}else {
			return new AnalysisExecution(AnalysisEnum.EMPTY_LIST);
		} 
	}
}
